<?php

namespace app\admin\model;

use think\Model;

/**
 * 需求
 * @package app\admin\model
 */
class Demand extends Model
{
    # 设置当前模型对应的完整数据表名称
    protected $name = 'resource_sale';

    /**
     * 本月需求数据
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function getDemandMonth($startTime, $endTime)
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereTime('create_time', '>=', $startTime)
            ->whereTime('create_time', '<', $endTime)
            ->group('create_time')
            ->where('status', '<>', -1)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 需求总数
     * @param array $where
     * @return int
     */
    public static function countDemand($where)
    {
        return self::where($where)->count();
    }

    /**
     * 今日新增需求
     * @return int
     */
    public static function todayDemand()
    {
        return self::whereDay('create_time')
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 本周新增需求
     * @return int
     */
    public static function countDemandWeek()
    {
        return self::whereWeek('create_time')
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 本月新增需求
     * @return int
     */
    public static function countDemandMonth()
    {
        return self::whereMonth('create_time')
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 需求列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getDemandList($where, $page, $pagesize)
    {
        $data = self::alias('d')
            ->join('location p', 'p.location_id = d.province', 'left')
            ->join('location c', 'c.location_id = d.city', 'left')
            ->field('d.*,p.name as province_name,c.name as city_name')
            ->where($where)
            ->page($page, $pagesize)
            ->order('d.create_time', 'desc')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 统计需求列表
     * @param $where
     * @return mixed
     */
    public static function countDemandList($where)
    {
        return self::alias('d')
            ->join('location p', 'p.location_id = d.province', 'left')
            ->join('location c', 'c.location_id = d.city', 'left')
            ->where($where)
            ->count();
    }

    /**
     * 获取最后一个自增id
     * @return mixed
     */
    public static function findDemandLastId()
    {
        return self::order('id', 'desc')->value('id');
    }

    /**
     * 单需求数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findDemand($where)
    {
        $info = self::where($where)->find();
        return $info ? $info->toArray() : [];
    }

    /**
     * 更新需求数据
     * @param $data
     * @param $where
     * @return Demand
     */
    public static function updateDemand($data, $where)
    {
        return self::update($data, $where);
    }

    /**
     * 创建需求数据
     * @param $data
     * @return Demand|Model
     */
    public static function createDemand($data)
    {
        return self::create($data);
    }

    /**
     * 需求总金额
     * @param $where
     * @return float
     */
    public static function sumDemandPrice($where)
    {
        return self::where($where)->sum('price');
    }

}